import {
	Tools,
	Modal,
} from 'weaver-mobile';
import React, {
	Component
} from 'react';
import ReactDOM from 'react-dom';
const getLabel = Tools.getLabel;

const isWeakPassword = (params = {}) => Tools.callApi({
	url:'/api/hrm/password/isWeakPassword',
	method:'POST',
	params
});

export const checkWeakPass = (password,onOk,onCancel,onSuccess) => {
	isWeakPassword({
		password
	}).then(datas => {
		const {
			weakPasswordDisable,
			isWeakPassword,
			defaultPasswordEnable
		} = datas;

		if ((weakPasswordDisable === "1") && isWeakPassword) {
			const div = document.createElement("div");
    		document.body.appendChild(div);

			ReactDOM.render(<PassConfirm 
				weakPasswordDisable={weakPasswordDisable}
				onOk={onOk}
				onCancel={onCancel}
			/>,div);
		} else {
			onSuccess();
		}
	}).catch(() => {
		onSuccess(); 
	})
}

class PassConfirm extends Component{
	constructor(props){
		super(props);

		this.state = {
			visible:false
		}
	}

	componentDidMount() {
		this.setState({visible:true});
	}

	handleOk = () => {
		const {onOk}= this.props;
		this.setState({visible:false});
		onOk && onOk();
	}

	handleCancel = () => {
		const {onCancel} = this.props;
		this.setState({visible:false});
		onCancel && onCancel();
	}

	render() {
		const {
			visible
		} = this.state, {
			weakPasswordDisable
		} = this.props;

		if (!visible) {
			return null;
		}

		const btns = [{
			text: getLabel(33703, '确定'),
			onPress: this.handleOk,
		}];

		if (weakPasswordDisable === "0") {
			btns.push({ text: getLabel(32694, '取消'), onPress: this.handleCancel });
		}


		return (
			<Modal 
				closable
				transparent
				visible={visible}
				onClose={this.handleCancel}
				title={getLabel(131329, '信息确认')}
				footer={btns}
			>
              <p >
                 {getLabel('516219','该密码安全性较低，管理员已开启弱密码禁止保存设置，请修改密码。') }
              </p>
			</Modal>
		)
	}
}

